//
//  XQLoginPhoneView.swift
//  Spark Peso
//
//  Created by 小琦 on 2025/2/4.
//

import UIKit

class XQLoginPhoneView: UIView {
    
    var textFielHandle: ((String?) -> Void)?
    
    private lazy var countryImageView : UIImageView = {
        let imageView = UIImageView()
        return imageView
    }()

    private lazy var prefixLb : UILabel = {
        let lb = UILabel()
        lb.textColor = .xq_black
        lb.font = UIFont.systemFont(ofSize: 20, weight: .medium)
        lb.setContentHuggingPriority(.required, for: .horizontal)
        lb.setContentCompressionResistancePriority(.required, for: .horizontal)
        return lb
    }()
    
    private lazy var lineView : UIView = {
        let view = UIView()
        view.backgroundColor = .xq_black
        return view
    }()
    
    private lazy var phoneTextField : UITextField = {
        let textField = UITextField()
        textField.font = UIFont.systemFont(ofSize: 20, weight: .medium)
        textField.keyboardType = .numberPad
        textField.textColor = .xq_black
        textField.attributedPlaceholder = NSAttributedString(string: "Enter your phone number",
                                                              attributes: [
                            NSAttributedString.Key.font: UIFont.systemFont(ofSize: 20),
                            NSAttributedString.Key.foregroundColor: UIColor(hex: "#A3A6A9")!])
        textField.addTarget(self, action: #selector(textFieldDidChange(_:)), for: .editingChanged)
        return textField
    }()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        makeUI()
        makeConstraints()
    }
    
    required init?(coder: NSCoder) {
        super.init(coder: coder)
        makeUI()
        makeConstraints()
    }
    
    private func makeUI() {
        initData()
        
        self.layer.cornerRadius = 30
        self.backgroundColor = UIColor(hex: "#F6F6F6")
        self.addSubview(self.prefixLb)
        self.addSubview(self.lineView)
        self.addSubview(self.phoneTextField)
    }
    
    private func makeConstraints() {
        self.prefixLb.snp.makeConstraints { make in
            make.centerY.equalToSuperview()
            make.left.equalTo(20)
        }
        self.lineView.snp.makeConstraints { make in
            make.centerY.equalToSuperview()
            make.left.equalTo(self.prefixLb.snp.right).offset(5)
            make.height.equalTo(23)
            make.width.equalTo(1)
        }
        self.phoneTextField.snp.makeConstraints { make in
            make.left.equalTo(self.lineView.snp.right).offset(10)
            make.centerY.equalToSuperview()
            make.right.equalTo(0)
            make.top.bottom.equalTo(0)
        }
    }
    
    private func initData() {
        if let phone = XQInitManager.shared.phoneCode() {
            self.prefixLb.text = "\(phone)"
        }
    }
    
    @objc private func textFieldDidChange(_ textField: UITextField) {
        textFielHandle?(textField.text)
    }
}
